package com.ljl.system.controller;


import com.ljl.basic.util.AjaxResult;
import com.ljl.basic.util.PageList;
import com.ljl.system.domain.DictionaryDetail;
import com.ljl.system.domain.DictionaryType;
import com.ljl.system.query.DictionaryTypeQuery;
import com.ljl.system.service.IDictionaryDetailService;
import com.ljl.system.service.IDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/DictionaryType")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class DictionaryTypeController {
    @Autowired
    private IDictionaryTypeService dictionaryTypeService;
    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    // 查询单个
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public DictionaryType queryById(@PathVariable("id") Long id){
        return dictionaryTypeService.findById(id);
    }

    // 查询所有
    @GetMapping()
    @ApiOperation(value = "查询全部" )
    public List<DictionaryType> queryAll(){
        return dictionaryTypeService.findAll();
    }

    // 删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            List<DictionaryDetail> detailServiceAll = dictionaryDetailService.findAll();
            for (DictionaryDetail detail : detailServiceAll) {
                if (detail.getTypes_id() == id) {
                    dictionaryDetailService.del(detail.getId());
                }
            }
            dictionaryTypeService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙请稍后重试！！");
        }
    }

    // 分页和高级查询
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<DictionaryType> queryPage(@RequestBody DictionaryTypeQuery dictionaryTypeQuery){
        return dictionaryTypeService.queryPage(dictionaryTypeQuery);
    }

    // 修改或添加
    @PutMapping
    @ApiOperation(value = "添加或修改" )
    public AjaxResult addOrUpdate(@RequestBody DictionaryType dictionaryType){
        try {
            if (dictionaryType.getId() != null) {
                dictionaryTypeService.update(dictionaryType);
            } else {
                dictionaryTypeService.save(dictionaryType);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙请稍后重试！！");
        }
    }
    // 批量删除
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids) {
        try {
            dictionaryTypeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙请稍后重试！！");
        }
    }

}
